import os
import sys
import time
import datetime

AtD = os.path.join(sys.path[0], 'FaceRec/DBFace/atomic_pid.txt')
ApF = os.path.join(sys.path[0], 'FaceRec/FaceRec/Faces_Temp/app_pid.txt')
AtF = os.path.join(sys.path[0], 'FaceRec/FaceRec/Faces_Temp/atomic_pid.txt')
ApM = os.path.join(sys.path[0], 'MotionDet/M_Temp/app_pid.txt')
AtM = os.path.join(sys.path[0], 'MotionDet/M_Temp/atomic_pid.txt')
ApP = os.path.join(sys.path[0], 'PedestrianDet/P_Temp/app_pid.txt')
AtP = os.path.join(sys.path[0], 'PedestrianDet/P_Temp/atomic_pid.txt')

pid_path = [AtD, ApF, AtF, ApM, AtM, ApP, AtP]
log_path = 'pid.txt'

if __name__ == '__main__':
    pid_list = ['0'] * len(pid_path)
    while True:
        for i in range(len(pid_path)):
            try:
                with open(pid_path[i], 'r') as f:
                    pid_list[i] = f.readlines()[0].strip()
            except FileNotFoundError as e:
                print(repr(e))
        process = os.popen('ps -ef | grep python')
        current_pid_list = process.readlines()
        alive = [False] * len(pid_path)
        for i, pid in enumerate(pid_list):
            for current_pid in current_pid_list:
                if pid in current_pid:
                    alive[i] = True
                    break
        info = [
            ' '.join(
                [
                    str(datetime.datetime.now()),
                    str(alive) + '\n'
                ]
            )
        ]
        info += [
            ' '.join(
                [
                    str(datetime.datetime.now()),
                    str(pid_list) + '\n'
                ]
            )
        ]
        with open(log_path, 'a') as f:
            f.writelines(info)
        time.sleep(120)
